package study.io.springboot.upload.support.utils;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Arrays;
import java.util.List;

public class CopyUtils {

    public static List<String> srcLt = Arrays.asList("https://ucanos.xdf.cn/troy/third/prod_source/office/cloudPrivate/cuijing16/2b218866-d978-478d-9c9d-16be947a0617.pdf"
            , "https://ucanos.xdf.cn/troy/third/prod_source/office/cloudPrivate/cuijing16/2b218866-d978-478d-9c9d-16be947a0617.pdf"
            , "https://ucanos.xdf.cn/troy/third/prod_source/office/cloudPrivate/cuijing16/2b218866-d978-478d-9c9d-16be947a0617.pdf"
            , "https://ucanos.xdf.cn/troy/third/prod_source/office/cloudPrivate/cuijing16/2b218866-d978-478d-9c9d-16be947a0617.pdf"
            //, "https://ucanos.xdf.cn/troy/third/prod_source/office/cloudPrivate/cuijing16/2b218866-d978-478d-9c9d-16be947a0617.pdf"
    );

    /**
     * 普通拷贝
     *
     * @param url  URL
     * @param dest String
     */
    public static void copyURLToFile(URL url, String dest) {
        //
        try (InputStream is = url.openStream()) {
            //
            FileUtil.writeFromStream(is, new File(dest));
        } catch (Exception ex) {
            throw new RuntimeException("拷贝文件异常: ", ex);
        }
    }

    /**
     * 普通拷贝
     *
     * @param src  String
     * @param dest String
     */
    public static void copy(String src, String dest) {
        try (FileInputStream fis = new FileInputStream(src)) {
            FileUtil.writeFromStream(fis, new File(dest));
        } catch (Exception ex) {
            throw new RuntimeException("拷贝文件异常: ", ex);
        }
    }

    /**
     * 零拷贝
     *
     * @param url  URL
     * @param dest String
     */
    public static void zeroCopyURLToFile(URL url, String dest) {
        try (InputStream is = url.openStream();
             //输入通道
             ReadableByteChannel isChannel = Channels.newChannel(is);
             //输出通道
             FileOutputStream fos = new FileOutputStream(dest);
             FileChannel osChannel = fos.getChannel()) {

            //拷贝
            osChannel.transferFrom(isChannel, 0, Long.MAX_VALUE);
        } catch (Exception ex) {
            throw new RuntimeException("拷贝文件异常: ", ex);
        }
    }

    /**
     * 零拷贝
     *
     * @param src  String
     * @param dest String
     */
    public static void zeroCopy(String src, String dest) {
        try (FileInputStream fis = new FileInputStream(src);
             //输入通道
             ReadableByteChannel channel = Channels.newChannel(fis);
             //输出通道
             FileOutputStream fos = new FileOutputStream(dest);
             FileChannel fileChannel = fos.getChannel()) {

            //拷贝
            fileChannel.transferFrom(channel, 0, Long.MAX_VALUE);
        } catch (Exception ex) {
            throw new RuntimeException("拷贝文件异常: ", ex);
        }
    }
}
